let ue = UE.getEditor("remark");
$(function () {
    jeDate("#purchaseTime", {
        format: "YYYY-MM-DD"
    });
    $("#createbut").unbind("click").click(function () {
        addFixedAssets();
    })
    createStorageList();
    $.ajax({
        url: "/ret/fixedassetsget/getFixedAssetSortTree",
        type: "post",
        dataType: "json",
        success: function (data) {
            var topNode = [{
                sortName: "TOP分类",
                isParent: "false",
                sortId: ""
            }];
            var newTreeNodes = topNode.concat(data);
            $.fn.zTree.init($("#menuTree"), setting1, newTreeNodes);
        }
    });
    $("#sortId").unbind("click").click(function (e) {
        e.stopPropagation();
        $("#menuContent").css({
            "width": $(this).outerWidth() + "px"
        }).slideDown(200);
    });
    $("body").unbind("click").click(function () {
        $("#menuContent").hide();
    });

    $("#menuContent").unbind("click").click(function (e) {
        e.stopPropagation();
    });
});

var setting1 = {
    async: {
        enable: true,// 设置 zTree 是否开启异步加载模式
        url: "/ret/fixedassetsget/getFixedAssetSortTree",// Ajax 获取数据的 URL 地址
        autoParam: ["sortId"],// 异步加载时需要自动提交父节点属性的参数
    },
    view: {
        dblClickExpand: false,
        selectedMulti: false
        //禁止多选
    },
    data: {
        simpleData: {
            enable: true,
            idKey: "sortId",
            pIdKey: "sortLevel",
            rootPId: "0"
        },
        key: {
            name: "sortName"
        }
    },
    callback: {
        onClick: function (e, treeId, treeNode) {
            var zTree = $.fn.zTree.getZTreeObj("menuTree"), nodes = zTree.getSelectedNodes(), v = "";
            vid = "";
            nodes.sort(function compare(a, b) {
                return a.id - b.id;
            });
            for (var i = 0, l = nodes.length; i < l; i++) {
                v += nodes[i].sortName + ",";
                vid += nodes[i].sortId + ",";
            }
            if (v.length > 0)
                v = v.substring(0, v.length - 1);
            var nameem = $("#sortId");
            nameem.val(v);
            if (vid.length > 0)
                vid = vid.substring(0, vid.length - 1);
            nameem.attr("data-value", vid);
        }
    }
};

function addFixedAssets() {
    if ($("#assetsCode").val() == "") {
        layer.msg("资产编号不能为空");
        return;
    }
    $.ajax({
        url: "/set/fixedassetsset/insertFixedAssets",
        type: "post",
        dataType: "json",
        data: {
            sortNo: $("#sortNo").val(),
            assetsName: $("#assetsName").val(),
            assetsCode: $("#assetsCode").val(),
            brand: $("#brand").val(),
            model: $("#model").val(),
            ownDept: $("#ownDept").attr("data-value"),
            purchasePrice: $("#purchasePrice").val(),
            purchaseTime: $("#purchaseTime").val(),
            depreciation: $("#depreciation").val(),
            sortId: $("#sortId").attr("data-value"),
            storageId: $("#storageId").val(),
            remark: ue.getContent(),
            attach: $("#fixedattach").attr("data_value")
        },
        success: function (data) {
            if (data.status == "200") {
                layer.msg(sysmsg[data.msg]);
                window.location.reload();
            } else if (data.status == "100") {
                layer.msg(sysmsg[data.msg]);
            } else {
                console.log(data.msg);
            }
        }
    });
}

function createStorageList() {
    $.ajax({
        url: "/ret/fixedassetsget/getAllFixedAssetsStorageList",
        type: "post",
        dataType: "json",
        success: function (data) {
            if (data.status == "200") {
                var html = "<option value='0'>请选择</option>";
                for (var i = 0; i < data.list.length; i++) {
                    html += "<option value=\"" + data.list[i].storageId + "\">" + data.list[i].storageName + "</option>";
                }
                $("#storageId").html(html);
            } else if (data.status == "100") {
                layer.msg(sysmsg[data.msg]);
            } else {
                console.log(data.msg);
            }
        }
    });
}
